package com.appcan.sync.common;

import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantLock;

import javax.servlet.Servlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Trigger;

import com.appcan.base.MerpConst;


public class SchedulerUtil {

	private static final Log log = LogFactory.getLog(SchedulerUtil.class);

	private static Properties quertz_properties = new Properties();
	
	private static Properties quertz_job_properties = new Properties();
	
	private static Properties quertz_type_properties = new Properties();
	
	private static Properties quertz_trigger_properties = new Properties();
	private static Map<String,ReentrantLock> lockmap = new HashMap<String,ReentrantLock>();
	
	/**
	 * ����quartz.properties
	 * @return
	 */
	public static Properties loadQuartzProperty() {
		try {
			quertz_properties.load(locateXpadConfigStream(MerpConst.XPAD_SCHEDULER_QUARTZPROPERTIES));
		}
		catch (Exception e) {
			log.error("����quartz.properties�����쳣." + e.toString());
		}
		return quertz_properties;
	}
	public static void setLock(String key){
		if(lockmap.get("key")==null){
			lockmap.put(key, new ReentrantLock());
		}
	}

	public static void loadInitProperties(){
		try {
			quertz_properties.load(locateXpadConfigStream(MerpConst.XPAD_SCHEDULER_QUARTZPROPERTIES));
			quertz_job_properties.load(locateXpadConfigStream(MerpConst.XPAD_SCHEDULER_JOBS));
			quertz_trigger_properties.load(locateXpadConfigStream(MerpConst.XPAD_SCHEDULER_TRIGGERS));
			quertz_type_properties.load(locateXpadConfigStream(MerpConst.XPAD_SCHEDULER_Type));
		}
		catch (Exception e) {
			log.error("load quartz.properties error" + e.toString(),e);
		}
	}
	public static InputStream locateXpadConfigStream(String filename)throws Exception {
		InputStream is = null;
		try {
			String basePath = Servlet.class.getClassLoader().getResource("")
					.toString();
			URL fileurl = new URL(basePath + filename);
			if (fileurl != null) {
				is = fileurl.openStream();
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("读取pad配置文件 " + filename + " 发生异常. " + e.toString());
					}
		return is;
}
	
	/**
	 * ���jobname����job������
	 * @param jobname
	 * @return
	 */
	public static JobDetail buildJobDetailByName(String jobname) {
		JobDetail jobdetail = null;
		try {
			quertz_job_properties.load(locateXpadConfigStream(MerpConst.XPAD_SCHEDULER_JOBS));
			String jobClassStr = quertz_job_properties.getProperty(jobname);
			Class jobClass = Class.forName(jobClassStr);
			jobdetail = new JobDetail(jobname, jobname + "_GROUP", jobClass);
		}
		catch (Exception e) {
			log.error("����JobDetail�����쳣." + e.toString());
		}
		return jobdetail;
	}
	
	/**
	 * ���jobname����Trigger������
	 * @param jobname
	 * @return
	 */
	public static Trigger buildTriggerByJobName(String jobname) {
		CronTrigger trigger = null;
		try {
			quertz_trigger_properties.load(locateXpadConfigStream(MerpConst.XPAD_SCHEDULER_TRIGGERS));
			String corn = quertz_trigger_properties.getProperty(jobname);
			trigger = new CronTrigger(jobname + "_TRIGGER", null, corn);
		}
		catch (Exception e) {
			log.error("����Trigger�����쳣." + e.toString());
		}
		return trigger;
	}
	public static Properties getQuertz_job_properties() {
		return quertz_job_properties;
	}
	public static void setQuertz_job_properties(Properties quertz_job_properties) {
		SchedulerUtil.quertz_job_properties = quertz_job_properties;
	}
	public static Properties getQuertz_trigger_properties() {
		return quertz_trigger_properties;
	}
	public static void setQuertz_trigger_properties(
			Properties quertz_trigger_properties) {
		SchedulerUtil.quertz_trigger_properties = quertz_trigger_properties;
	}
	public static Properties getQuertz_type_properties() {
		return quertz_type_properties;
	}
	public static void setQuertz_type_properties(Properties quertz_type_properties) {
		SchedulerUtil.quertz_type_properties = quertz_type_properties;
	}
		
}
